/**
 * 
 */
package de.cbf.cam.dsa.model.entities.weapon;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

import de.cbf.cam.dsa.model.entities.WeaponStyle;
import de.cbf.cam.dsa.model.persistence.Header;

/**
 * @author Nebelritter
 *
 */
@Entity
public class WeaponMeleeInUse extends Header implements WeaponInUse{
	/**
	 * @uml.property  name="weapon"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private WeaponMelee weapon;
	
	/**
	 * @uml.property  name="activeStyle"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	protected WeaponStyle activeStyle;
	
	/**
	 * @uml.property  name="aT"
	 */
	protected int AT = 0;
	/**
	 * @uml.property  name="pA"
	 */
	protected int PA = 0;
	
	/**
	 * @uml.property  name="tpDiceNumber"
	 */
	protected int tpDiceNumber;
	/**
	 * @uml.property  name="tpDiceSides"
	 */
	protected int tpDiceSides;
	/**
	 * @uml.property  name="tpBonus"
	 */
	protected int tpBonus;
	
	public static final String PROPERTYNAME ="WeaponMelee";
	/**
	 * @uml.property  name="propertyChangeSupport"
	 */
	public PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
	
	public WeaponMeleeInUse(WeaponMelee weapon,WeaponStyle style) {
		super();
		this.weapon 		= weapon;
		this.activeStyle 	= style;
		if(weapon!=null){
			weapon.propertyChangeSupport.addPropertyChangeListener(new PropertyAdvanceListener());
		}
	}
	@Override
	public boolean isSameAs(WeaponInUse weaponInUse) {
		return weapon.equals(weaponInUse.getWeapon());
	}
	/**
	 * @return
	 * @uml.property  name="propertyChangeSupport"
	 */
	@Override
	@Transient
	public PropertyChangeSupport getPropertyChangeSupport() {
		return propertyChangeSupport;
	}
	@Override
	@Id
	public String getGuid() {
		return guid;
	}

	@Override
	protected void setGuid(String guid) {
		this.guid = guid;		
	}
	
	/**
	 * @return
	 * @uml.property  name="activeStyle"
	 */
	@OneToOne(cascade=CascadeType.ALL)
	public WeaponStyle getActiveStyle(){
		return activeStyle;
	}


	/**
	 * @param style
	 * @uml.property  name="activeStyle"
	 */
	public void setActiveStyle(WeaponStyle style){
		if(weapon.getStyles().contains(style)){
			this.activeStyle=style;
		}	
	}
	/**
	 * @return
	 * @uml.property  name="weapon"
	 */
	@OneToOne(cascade=CascadeType.ALL)
	public WeaponMelee getWeapon(){
		return weapon;
	}
	/**
	 * @param weapon
	 * @uml.property  name="weapon"
	 */
	protected void setWeapon(WeaponMelee weapon){
		this.weapon = weapon;
	}
	
	
	
	

	/**
	 * @return the aT
	 */
	public int getATtack() {
		return AT;
	}

	/**
	 * @return the pA
	 */
	public int getPArade() {
		return PA;
	}

	/**
	 * @param aT the aT to set
	 */
	public void setATtack(int aT) {
		AT = aT;
	}

	/**
	 * @param pA the pA to set
	 */
	public void setPArade(int pA) {
		PA = pA;
	}
	/**
	 * @return  the tpDiceNumber
	 * @uml.property  name="tpDiceNumber"
	 */
	public int getTpDiceNumber() {
		return tpDiceNumber;
	}
	/**
	 * @param tpDiceNumber  the tpDiceNumber to set
	 * @uml.property  name="tpDiceNumber"
	 */
	public void setTpDiceNumber(int tpDiceNumber) {
		this.tpDiceNumber = tpDiceNumber;
		propertyChangeSupport.firePropertyChange(PROPERTYNAME+"tpDiceNumber",null,tpDiceNumber);
	}
	/**
	 * @return  the tpDiceSides
	 * @uml.property  name="tpDiceSides"
	 */
	public int getTpDiceSides() {
		return tpDiceSides;
	}
	/**
	 * @param tpDiceSides  the tpDiceSides to set
	 * @uml.property  name="tpDiceSides"
	 */
	public void setTpDiceSides(int tpDiceSides) {
		this.tpDiceSides = tpDiceSides;
		propertyChangeSupport.firePropertyChange(PROPERTYNAME+"tpDiceSides",null,tpDiceSides);
	}
	/**
	 * @return  the tpBonus
	 * @uml.property  name="tpBonus"
	 */
	public int getTpBonus() {
		return tpBonus;
	}

	/**
	 * @param tpBonus  the tpBonus to set
	 * @uml.property  name="tpBonus"
	 */
	public void setTpBonus(int tpBonus) {
		this.tpBonus = tpBonus;
		propertyChangeSupport.firePropertyChange(PROPERTYNAME+"tpBonus",null,tpBonus);
	}
	private class PropertyAdvanceListener implements PropertyChangeListener{
		@Override
		public void propertyChange(PropertyChangeEvent event) {
			propertyChangeSupport.firePropertyChange(PROPERTYNAME+event.getPropertyName(),null,event.getNewValue());			
		}		
	}
}
